package com.A21MobileSales;
 
import java.util.Calendar;

import com.A21.Object.Global;
import com.A21.Object.MSTProduct;
import com.A21.Object.TrnDetail;
import com.A21MobileSales.R;
import com.A21MobileSales.DataBase.DatabaseHelper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View; 
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class TrnProductDetails extends Activity {

	DatabaseHelper objDataBase;
	Calendar myCalendar = Calendar.getInstance();
	StringBuilder strbuilder;
	String Id, Product, TrnType, Code, CName;
	EditText edtProduct, edtPName, edtQty, edtUnitPrice, edtDiscount, edtNotes;
	TextView tvDiscount;
	Button btnSave;
	TrnDetail detail;
	MSTProduct prod;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.trn_product_details);
		//getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, 520);
		try {
			objDataBase = new DatabaseHelper(this);
			if (getIntent().hasExtra("Id")) {
				Id = getIntent().getStringExtra("Id");
			}
			if (getIntent().hasExtra("Product")) {
				Product = getIntent().getStringExtra("Product");
			}
			if (getIntent().hasExtra("TrnType")) {
				TrnType = getIntent().getStringExtra("TrnType");
			}
			if (getIntent().hasExtra("Code")) {
				Code = getIntent().getStringExtra("Code");
			}
			if (getIntent().hasExtra("CName")) {
				CName = getIntent().getStringExtra("CName");
			}
			prod = objDataBase.GetProductByCode(Product);
			edtProduct = (EditText) findViewById(R.id.edtProduct);
			edtPName = (EditText) findViewById(R.id.edtPName);
			edtQty = (EditText) findViewById(R.id.edtQty);
			edtUnitPrice = (EditText) findViewById(R.id.edtUnitPrice);
			edtDiscount = (EditText) findViewById(R.id.edtDiscount);
			tvDiscount= (TextView) findViewById(R.id.tvDiscount);
			if(!Global.getMSTSettings().LineDiscount){
				edtDiscount.setVisibility(View.GONE); 
				tvDiscount.setVisibility(View.GONE); 
			}
			 
			edtDiscount.setFilters(new InputFilter[]{ new InputFilterMinMax("1", "100")});
			
			edtNotes = (EditText) findViewById(R.id.edtNotes);
			if (!Id.equals("0")) {
				detail = Global.getSelectedTrnDetail(); 
			} else {
				detail = new TrnDetail();
				detail.Product=Product;
				detail.LineDiscount1 = 0;
				detail.Conversion = 1;
				detail.PName = prod.PName;
				detail.ProdDesc = prod.PName;
				detail.UOM = prod.SalesUnit;
				detail.OrderQuantity=1;
				detail.StockUnit = prod.StockUnit;
				detail.isTrue = false;
				detail.TrnType = TrnType; 
				Common objC=new Common(TrnProductDetails.this);
				detail=objC.GetProductPrice(Code, detail); 
				if (!detail.UOM.equals(detail.StockUnit)) { 
					 detail=objC.GetConversionFactor(detail);
				}
			} 
			edtProduct.setText(detail.Product);
			edtPName.setText(detail.ProdDesc);
			edtQty.setText(String.valueOf(detail.OrderQuantity));
			edtUnitPrice.setText(String.valueOf(detail.UnitPrice));
			edtDiscount.setText(String
					.valueOf(detail.LineDiscount1 * 100)); 
			
			edtNotes.setText(detail.DetailMemo==null?"":detail.DetailMemo);
			edtDiscount.setEnabled(Global.getMSTSettings().DiscountLock);  
			
			edtQty.addTextChangedListener(new TextWatcher() {
				public void afterTextChanged(Editable s) {
					String qty = edtQty.getText().toString();
                     if(isNumeric(qty)){
                    	 Common objC=new Common(TrnProductDetails.this);
                    	 try {
							detail=objC.GetProductPrice(Code, detail);
							detail=objC.GetConversionFactor(detail);
						} catch (Exception e) { 
							Toast.makeText(TrnProductDetails.this, e.getMessage(), Toast.LENGTH_SHORT) .show();
						}
                     }
				} 
				@Override
				public void beforeTextChanged(CharSequence s, int start,
						int count, int after) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onTextChanged(CharSequence s, int start,
						int before, int count) {
					// TODO Auto-generated method stub

				}
			});

			btnSave = (Button) findViewById(R.id.btnSave);
			btnSave.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					hideSoftKeyboard(TrnProductDetails.this,v);
					if (edtQty.getText().toString().equals("")) {
						Toast.makeText(TrnProductDetails.this,
								"Please enter the quantity", Toast.LENGTH_SHORT)
								.show();
						return;
					}
					double up=0;
					if (!edtUnitPrice.getText().toString().equals("")) {
						up=Double.valueOf(edtUnitPrice.getText().toString());
					}
					double val=0;
					if (!edtDiscount.getText().toString().equals("") && up>0) {
						val=Double.valueOf(edtDiscount.getText().toString());
					}
					detail.Product = edtProduct.getText().toString();
					detail.ProdDesc = edtPName.getText().toString();
					detail.DetailMemo = edtNotes.getText().toString(); 
					double discPercent=val/100;
					detail.LineDiscount1=discPercent;
					detail.OrderQuantity = Double.valueOf(edtQty.getText()
							.toString());
					detail.UnitPrice = up;
					Global.setSelectedTrnDetail(detail);
					Bundle b = new Bundle();
					b.putString("status", "done");
					Intent intent = new Intent();
					intent.putExtras(b);
					setResult(RESULT_OK, intent);
					finish();
				}
			});

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void hideSoftKeyboard (Activity activity, View view) 
	{
	    InputMethodManager imm = (InputMethodManager)activity.getSystemService(activity.INPUT_METHOD_SERVICE);
	    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
	}
	
	public static boolean isNumeric(String str) {
		try {
			double d = Double.parseDouble(str);
		} catch (NumberFormatException nfe) {
			return false;
		}
		return true;
	}

	// CalCulation for Discount from price
	public void funDiscountCalculation() {
		try {

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
